import random
import numpy as np
import logging
import sys

is_logger_model_init=False

def train_valid_split(x,y,detail,train_ratio=0.8,random_seed=None):
    if random_seed:
        random.seed(random_seed)
        
    sample_number=len(x)
    
    shuffle_indexes=random.sample(list(range(sample_number)),sample_number)
    
    train_number=int(sample_number*train_ratio)
    train_x=x[shuffle_indexes[:train_number],:]
    train_y=y[shuffle_indexes[:train_number],:] if len(np.shape(y))>1 else  y[shuffle_indexes[:train_number]]
    train_detail=[detail[i] for i in shuffle_indexes[:train_number]]  
    
    val_x=x[shuffle_indexes[train_number:],:]
    val_y=y[shuffle_indexes[train_number:],:] if len(np.shape(y))>1 else  y[shuffle_indexes[train_number:]]
    val_detail=[detail[i] for i in shuffle_indexes[train_number:]] 
    
    return (train_x,train_y,train_detail),(val_x,val_y,val_detail)

def log(text):
    global is_logger_model_init
    if is_logger_model_init == False:
        logging.basicConfig(filename='tasks.log',filemode='w',format='%(message)s',level=logging.DEBUG)
        logging.info(f"running file {sys.argv[0]}....")
        is_logger_model_init=True

    print(text)
    logging.info(text)